
package com.google.code.netbuilder.services.xmlaccess;

import com.google.code.netbuilder.dal.entities.Cable;
import com.google.code.netbuilder.dal.entities.Equipment;
import com.google.code.netbuilder.dal.entities.ManagedObject;
import com.google.code.netbuilder.dal.providers.ICableProvider;
import com.google.code.netbuilder.dal.providers.IEquipmentProvider;
import com.google.code.netbuilder.util.ManagedObjectDataModel;
import com.google.code.netbuilder.xmlaccess.XmlAccessor;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.xml.bind.JAXBException;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Андрей
 */

@ManagedBean(name="XMLExportService")
@ViewScoped
public class XMLExportService implements Serializable {
    
    @EJB
    private IEquipmentProvider ep;
    
    @EJB
    private ICableProvider cp;
    
    private List<Equipment> equipments;
    
    private List<Cable> cables;
    
    private ManagedObjectDataModel<Equipment> equipDM;
    
    private ManagedObjectDataModel<Cable> cableDM;
    
    private Equipment[] selectedEquipments;
    
    private Cable[] selectedCables;
    
    private StreamedContent XMLFile;
    
    private String filename;
    
    @PostConstruct
    public void initialize() {
        
        equipments = ep.findAll();
        cables = cp.findAll();
        
        equipDM = new ManagedObjectDataModel<Equipment>(equipments);
        cableDM = new ManagedObjectDataModel<Cable>(cables);
    }
    
    public void formXMLFile() {
        

        
        List<ManagedObject> finalList = new ArrayList<ManagedObject>();
        finalList.addAll(Arrays.asList(selectedEquipments));
        finalList.addAll(Arrays.asList(selectedCables));
        
        if(finalList.isEmpty())
        {
           FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "No elements selected");
           FacesContext.getCurrentInstance().addMessage(null, message);
           return;
        }
        filename = "NetPart " + new Date().toString() + ".xml";
        
        try {
            
            XmlAccessor accessor = XmlAccessor.getInstance();
            InputStream is = new ByteArrayInputStream(accessor.marshalToString(finalList).getBytes());
            XMLFile = new DefaultStreamedContent(is, "text/xml", filename);
        }
        catch(JAXBException ex) {
            
           FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Unable to marshall");
           FacesContext.getCurrentInstance().addMessage(null, message);
        }
        
    }

    public StreamedContent getXMLFile() {
        return XMLFile;
    }

    public void setXMLFile(StreamedContent XMLFile) {
        this.XMLFile = XMLFile;
    }

    public ManagedObjectDataModel<Cable> getCableDM() {
        return cableDM;
    }

    public void setCableDM(ManagedObjectDataModel<Cable> cableDM) {
        this.cableDM = cableDM;
    }

    public List<Cable> getCables() {
        return cables;
    }

    public void setCables(List<Cable> cables) {
        this.cables = cables;
    }

    public ICableProvider getCp() {
        return cp;
    }

    public void setCp(ICableProvider cp) {
        this.cp = cp;
    }

    public IEquipmentProvider getEp() {
        return ep;
    }

    public void setEp(IEquipmentProvider ep) {
        this.ep = ep;
    }

    public ManagedObjectDataModel<Equipment> getEquipDM() {
        return equipDM;
    }

    public void setEquipDM(ManagedObjectDataModel<Equipment> equipDM) {
        this.equipDM = equipDM;
    }

    public List<Equipment> getEquipments() {
        return equipments;
    }

    public void setEquipments(List<Equipment> equipments) {
        this.equipments = equipments;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public Cable[] getSelectedCables() {
        return selectedCables;
    }

    public void setSelectedCables(Cable[] selectedCables) {
        this.selectedCables = selectedCables;
    }

    public Equipment[] getSelectedEquipments() {
        return selectedEquipments;
    }

    public void setSelectedEquipments(Equipment[] selectedEquipments) {
        this.selectedEquipments = selectedEquipments;
    }
}
